//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConstrainedEvolver.h"
using namespace Cephei::QL::Models::Marketmodels;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelEvolver.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (boost::shared_ptr<QuantLib::ConstrainedEvolver>& childNative, Object^ owner) : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
#ifdef HANDLE
	_phConstrainedEvolver = NULL;
#endif
	_ppConstrainedEvolver = &childNative;
    _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
}
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (QuantLib::ConstrainedEvolver& childNative, Object^ owner) : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
#ifdef HANDLE
	_phConstrainedEvolver = NULL;
#endif
	_ppConstrainedEvolver = new boost::shared_ptr<QuantLib::ConstrainedEvolver> (&childNative);
    _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
    _ConstrainedEvolverOwner = owner;
    _MarketModelEvolverOwner = owner;
}

Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (CConstrainedEvolver^ copy) : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
#ifdef HANDLE
	_phConstrainedEvolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstrainedEvolver = new boost::shared_ptr<QuantLib::ConstrainedEvolver> (copy->GetShared());
        _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
    }
}
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (PLATFORM::Type^ t) : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
#ifdef HANDLE
	_phConstrainedEvolver = NULL;
#endif
	if (!t->IsSubclassOf(CConstrainedEvolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (QuantLib::Handle<QuantLib::ConstrainedEvolver>& childNative, Object^ owner)  : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
	_phConstrainedEvolver = &childNative;
	_ppConstrainedEvolver = &static_cast<boost::shared_ptr<QuantLib::ConstrainedEvolver>>(childNative.currentLink());
    _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
    _ConstrainedEvolverOwner = owner;
}
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (QuantLib::Handle<QuantLib::ConstrainedEvolver> childNative)  : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
	_phConstrainedEvolver = &childNative;
	_ppConstrainedEvolver = &static_cast<boost::shared_ptr<QuantLib::ConstrainedEvolver>>(childNative.currentLink());
    _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::CConstrainedEvolver (QuantLib::ConstrainedEvolver childNative)  : CMarketModelEvolver(CConstrainedEvolver::typeid)
{
#ifdef HANDLE
	_phConstrainedEvolver = NULL;
#endif
	_ppConstrainedEvolver = new boost::shared_ptr<QuantLib::ConstrainedEvolver> (new QuantLib::ConstrainedEvolver (childNative));
    _ppMarketModelEvolver = new boost::shared_ptr<QuantLib::MarketModelEvolver> (boost::dynamic_pointer_cast<QuantLib::MarketModelEvolver> (*_ppConstrainedEvolver));
}
#endif

Cephei::QL::Models::Marketmodels::CConstrainedEvolver::~CConstrainedEvolver ()
{
    if (_ppConstrainedEvolver != NULL)
    {
	    delete _ppConstrainedEvolver;
        _ppConstrainedEvolver = NULL;
    }
}
Cephei::QL::Models::Marketmodels::CConstrainedEvolver::!CConstrainedEvolver ()
{
    if (_ppConstrainedEvolver != NULL)
    {
	    delete _ppConstrainedEvolver;
    }
}
QuantLib::ConstrainedEvolver& Cephei::QL::Models::Marketmodels::CConstrainedEvolver::GetReference ()
{
    if (_ppConstrainedEvolver == NULL) throw REFNEW NativeNullException ();
	return **_ppConstrainedEvolver;
}
boost::shared_ptr<QuantLib::ConstrainedEvolver>& Cephei::QL::Models::Marketmodels::CConstrainedEvolver::GetShared ()
{
    if (_ppConstrainedEvolver == NULL) throw REFNEW NativeNullException ();
	return *_ppConstrainedEvolver;
}
QuantLib::ConstrainedEvolver* Cephei::QL::Models::Marketmodels::CConstrainedEvolver::GetPointer ()
{
    if (_ppConstrainedEvolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstrainedEvolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstrainedEvolver>& Cephei::QL::Models::Marketmodels::CConstrainedEvolver::GetHandle ()
{
	if (_phConstrainedEvolver == NULL)
	{
		_phConstrainedEvolver = new Handle<QuantLib::ConstrainedEvolver> (*_ppConstrainedEvolver);
	}
	return *_phConstrainedEvolver;
}
#endif
bool Cephei::QL::Models::Marketmodels::CConstrainedEvolver::HasNative () 
{
	return (_ppConstrainedEvolver != NULL);
}

Cephei::QL::Models::Marketmodels::IConstrainedEvolver^ Cephei::QL::Models::Marketmodels::CConstrainedEvolver::SetConstraintType (Cephei::Core::IVector<UInt64>^ startIndexOfSwapRate, Cephei::Core::IVector<UInt64>^ EndIndexOfSwapRate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        startIndexOfSwapRate->Lock();
        INativeVector<UInt64>^ _NCIstartIndexOfSwapRate = startIndexOfSwapRate->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCstartIndexOfSwapRate = safe_cast<CUInt64Vector^>(_NCIstartIndexOfSwapRate);
        std::vector<QuantLib::Size>& _startIndexOfSwapRate = static_cast<std::vector<QuantLib::Size>&> (_NCstartIndexOfSwapRate->GetReference ());
        EndIndexOfSwapRate->Lock();
        INativeVector<UInt64>^ _NCIEndIndexOfSwapRate = EndIndexOfSwapRate->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCEndIndexOfSwapRate = safe_cast<CUInt64Vector^>(_NCIEndIndexOfSwapRate);
        std::vector<QuantLib::Size>& _EndIndexOfSwapRate = static_cast<std::vector<QuantLib::Size>&> (_NCEndIndexOfSwapRate->GetReference ());
    	(*_ppConstrainedEvolver)->setConstraintType ( _startIndexOfSwapRate,  _EndIndexOfSwapRate );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (startIndexOfSwapRate != nullptr) startIndexOfSwapRate->Unlock(); //NO2  
        if (EndIndexOfSwapRate != nullptr) EndIndexOfSwapRate->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

